package com.webull.information.center.carwler.job;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.webull.information.center.carwler.common.model.CollectRecord;
import com.webull.information.center.carwler.common.model.parm.CollectParm;
import com.webull.information.center.carwler.common.util.eljob.ElasticJobUtil;
import com.webull.information.center.carwler.dao.CollectRecordDao;
import com.webull.information.center.carwler.task.News_ReutersGuide_Task;
import com.webull.securities.client.service.RegionService;

/**
 * 路透新闻采集job
 * 
 * @author shimingjun
 * @date 2016年8月30日 下午5:05:19
 * @version 1.0
 * @since JDK 1.8
 */
@Component
public class News_ReutersGuideFund_Job implements SimpleJob {
	protected final Logger logger = LogManager.getLogger(getClass());
	private @Autowired News_ReutersGuide_Task news_ReutersGuide_Task;
	private @Autowired CollectRecordDao collectRecordDao;
	private @Autowired RegionService regionService;
	private final String collectJob = "news_ReutersGuide_fund_Job";
	private final Integer loop = 30;// 循环20次退出,获取 20*100

	// 中、港、美的英文从路透获取

	public static List<Integer> regionIds = new ArrayList<>();

	@PostConstruct
	private void initRegionIds() {
		Optional.ofNullable(regionService.searchAll()).ifPresent(rs -> {
			rs.forEach(rs0 -> regionIds.add(rs0.getId()));
		});
	}

	@Override
	public void execute(ShardingContext context) {
		int sharding = Optional.ofNullable(context).map(v -> v.getShardingItem()).orElse(-1);
		int totalCount = Optional.ofNullable(context).map(v -> v.getShardingTotalCount()).orElse(-1);
		logger.info("News_ReutersGuideFund_Job:" + ArrayUtils.toString(sharding) + ",totalCount:" + totalCount);
		List<Integer> shardingRegionIds = Optional.ofNullable(ElasticJobUtil.shardingItems(context, regionIds))
				.orElse(regionIds);
		logger.info("news_ReutersGuide_Job div shardingRegionIds:{}; ", ArrayUtils.toString(shardingRegionIds));
		if (shardingRegionIds != null && !shardingRegionIds.isEmpty()) {
			process_0(shardingRegionIds);
		}
	}

	public void process_0(List<Integer> shardingRegionIds) {
		// Collections.shuffle(shardingRegionIds);
		logger.info("news_ReutersGuide_Job div size{};shardExchanges:{};", ArrayUtils.toString(shardingRegionIds));
		for (Integer reginoId : shardingRegionIds) {
			try {
				List<CollectRecord> records = collectRecordDao.search(collectParm(reginoId));
				Integer lastId = getLastTickeId(records);
				logger.info("news_ReutersGuide_Job shardExchanges start collectJob{} div ,reginoId:{},lastId{}",
						collectJob, reginoId, lastId);
				Integer lastId_ = news_ReutersGuide_Task.fullReutersGuideFundNews(reginoId, lastId, loop);
				CollectRecord record = backRecord(reginoId);
				record.setLastTickerId(lastId_);
				Integer collId = collectRecordDao.add(record);
				logger.info("news_ReutersGuide_Job shardExchanges end collectJob{} div ,reginoId:{},collId{}",
						collectJob, reginoId, collId);
			} catch (Exception e) {
				logger.error("news_ReutersGuide_Job shardExchanges collectJob div error:" + reginoId, e);
			}

		}
	}

	private Integer getLastTickeId(List<CollectRecord> records) {
		if (records == null || records.isEmpty()) {
			return -1;
		} else {
			return records.get(0).getLastTickerId();
		}
	}

	private CollectParm collectParm(Integer regionId) {
		CollectParm parm = new CollectParm();
		parm.setCollectJob(collectJob);
		parm.setExchangeCode("region_code");
		parm.setExchangeId(regionId);
		parm.setRecordType(CollectRecord.RecordTypeConstants.NEWS);
		return parm;
	}

	private CollectRecord backRecord(Integer regionId) {
		CollectRecord record = new CollectRecord();
		record.setCollectJob(collectJob);
		record.setExchangeCode("region_code");
		record.setExchangeId(regionId);
		record.setRecordType(CollectRecord.RecordTypeConstants.NEWS);
		try {
			record.setCustomer(InetAddress.getLocalHost().getHostAddress());
		} catch (Exception e) {
		}
		return record;
	}


}
